//
//  SetDingShiViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 chenn. All rights reserved.
//

#import "SetDingShiViewController.h"
#import "SetDingShiTableViewCell.h"
#import "Player.h"
@interface SetDingShiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic ,strong)UITableView *tableView;
@property (nonatomic ,strong)NSMutableArray *arr;
@property (nonatomic ,strong)UIView *myView;
@property (nonatomic ,strong)UISwitch *sw;
@property (nonatomic ,strong)NSTimer *timer;
@property (nonatomic ,assign)NSInteger i;
@property (nonatomic ,strong)UILabel *lable;
@property (nonatomic ,strong)NSUserDefaults *uu;
@property (nonatomic ,assign)NSInteger cellRow;
@property (nonatomic ,strong)UITableViewCell *cell;
@end

@implementation SetDingShiViewController
-(void)viewWillAppear:(BOOL)animated
{
  
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.arr=[NSMutableArray arrayWithObjects:@"10分钟后",@"20分钟后",@"30分钟后",@"60分钟后",@"90分钟后", nil];
    
    self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource=self;
    self.tableView.delegate=self;
    
    [self.view addSubview:self.tableView];
    
    self.sw=[[UISwitch alloc]initWithFrame:CGRectMake(FM_Screen_Pts_Width*300, FM_Screen_Pts_Height*35, FM_Screen_Pts_Width*50, FM_Screen_Pts_Height*30)];
    
    [self.sw addTarget:self action:@selector(sw:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.tableView addSubview:self.sw];
    
    self.myView=[[UIView alloc]initWithFrame:CGRectMake(0, FM_Screen_Pts_Height*100, FM_Screen_Width,FM_Screen_Pts_Height*1000)];
    self.myView.backgroundColor=[UIColor whiteColor];
    self.myView.alpha=1;
    [self.tableView addSubview:self.myView];
    
    self.lable=[[UILabel alloc]initWithFrame:CGRectMake(FM_Screen_Pts_Width*200, FM_Screen_Pts_Height*35, FM_Screen_Pts_Width*50, FM_Screen_Pts_Height*30)];
    
    [self.tableView addSubview:self.lable];

    
    self.uu=[NSUserDefaults standardUserDefaults];
//    [self.uu setObject:@"" forKey:@"aaaAA"];
    
    if ([self.uu objectForKey:@"aaaAA"] == nil) {
        
        
        [self.sw setOn:NO animated:NO];
    }else{
        self.myView.alpha=0;
        [self.sw setOn:YES animated:NO];
        
    }
    
    self.cellRow=0;
    
}
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }else{
        return self.arr.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        SetDingShiTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"dingshi1"];
        if (!cell) {
            cell=[[SetDingShiTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dingshi1"];
        }
        cell.lable1.text=@"定时停止播放";
        
        //修改倒计时标签现实内容
        cell.lable3.text=self.lable.text;
        
        return cell;

    }else{
    SetDingShiTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"dingshi2"];
    if (!cell) {
        cell=[[SetDingShiTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dingshi2"];
        
        cell.lable1.text=[self.arr objectAtIndex:indexPath.row];
//        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    
    
    return cell;
        
    }
    return NULL;
    
}
//section 分区间隔
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   
    return FM_Screen_Pts_Height*20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return FM_Screen_Pts_Height*60;
}



-(void)sw:(UISwitch *)s
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    if (s.on) {
        [user setObject:@"a" forKey:@"aaaAA"];
        self.myView.alpha=0;
       
    }else{
        [user setObject:nil forKey:@"aaaAA"];
         self.i=0;
        [UIView animateWithDuration:0.2 animations:^{
           
            self.myView.alpha=1;
            
            [self.timer invalidate];
            self.lable.text=@"";
           

        }];
 
    
        
    }
    
    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section==1) {
        for (UITableViewCell *cell in [tableView visibleCells]) {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
        
        self.cell = [tableView cellForRowAtIndexPath:indexPath];
        self.cell.accessoryType = UITableViewCellAccessoryCheckmark;
 
    if (indexPath.section==1&&indexPath.row==0) {
        self.i=0;
         [self.timer invalidate];
        self.i=10*60;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];//repeats是否重复
        
    }else if (indexPath.section==1&&indexPath.row==1) {
         self.i=0;
         [self.timer invalidate];
        self.i=20 * 60;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];//repeats是否重复
        
           }else if (indexPath.section==1&&indexPath.row==2) {
         self.i=0;
        [self.timer invalidate];
        self.i=30 * 60;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];//repeats是否重复
        }else if (indexPath.section==1&&indexPath.row==3) {
         self.i=0;
        [self.timer invalidate];
        self.i=60 * 60;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];//repeats是否重复
           }else if (indexPath.section==1&&indexPath.row==4) {
         self.i=0;
        [self.timer invalidate];
        self.i=90 * 60;
        self.timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timer:) userInfo:nil repeats:YES];//repeats是否重复
           }
    }
}

-(void)timer:(NSTimer *)time
{
        self.i--;
        //倒计时-1
        
        NSInteger a=self.i/60;
        NSInteger b=self.i%60;
        
        
        //修改倒计时标签现实内容
        self.lable.text=[NSString stringWithFormat:@"%ld:%ld",(long)a,(long)b];
    
        NSLog(@"------%@", [NSString stringWithFormat:@"%ld",(long)self.i]);
        //当倒计时到0时，做需要的操作，比如验证码过期不能提交
        if(self.i==0){
            self.lable.text=@"";
            [self.sw setOn:NO];
            [self.timer invalidate];
            
            Player *player = [Player shareInstance];
            [player.audioPlayer pause];
            
            NSLog(@"asdasd");
        }
 
    
}
                           
  



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
